package Groep3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class WebsiteRipper {
	private ArrayList<String> urls;
	private String url; 
	
	public WebsiteRipper() throws URISyntaxException{
		urls = new ArrayList<String>();
		Scanner in= new Scanner(System.in);
		System.out.println("Voer website in");
		url = in.nextLine();
		rip();
		System.out.println("Website geript");
		in.close();
	}
	
	public String grabContent(URL url) throws IOException{
		URLConnection conn = url.openConnection();
		
		StringBuffer stringbuffer = new StringBuffer();
		BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream()));
		String inputline = null;
		
		while((inputline = in.readLine())!= null){
			stringbuffer.append(inputline);
		}
		in.close();
		//System.out.println(urls);
		return stringbuffer.toString();
	}
	
	public void rip() throws URISyntaxException{
		URL uRl = null;
		try {
			if(url.contains("http://")){
				uRl = new URL(url);
			}else{
			uRl = new URL("http://" + url);
			}
			String content = grabContent(uRl);
			//System.out.print(content);
			
			Pattern pattern = Pattern.compile("(src|href)=('|\")(([a-z0-9A-Z]|/|_|-|\\.)+)('|\"|<)" );
			// finds a viewstate value in the string with regular expression
			Matcher matcher = pattern.matcher(content);
			while (matcher.find()) { 
				//System.out.println(matcher);
				try{
					String newURI = new URI(url).resolve(new URI(matcher.group(3))).toString();
					urls.add(newURI);
					//System.out.println(newURI);
					// Geen externe links!
					if ( newURI.contains("http://") && !newURI.contains(uRl.getHost())) {
						continue;
					}
					try{
						grabContent(new URI(newURI).toURL());
					}catch(IllegalArgumentException e){
						e.getCause();
						
						System.err.println(newURI + " kan niet worden gedownload");
					}
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				save(uRl, content);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void save( URL url, String content ) throws IOException{
		File dir = new File(url.getHost());
	
		if ( !dir.exists() && !dir.mkdir() ){
			throw new IOException("Cannot create dir");
		}
	
		String path = url.getPath();
	
		// Omzetten naar .html
		path = path.replace(".php", ".html");
	
		// Voor elke slash, new dir maken
		// Laatste hoeft niet
		String[] splitted = path.split("/");
		if ( splitted.length > 0){
			splitted[splitted.length -1] = "";
			String dirPath = url.getHost() + "/";
			for( String part : splitted){
				if ( part.length() > 0 ){
					File dir2 = new File(dirPath + part);
				
				if ( !dir2.exists() && !dir2.mkdir() )
					throw new IOException("Cannot create dir");
				
				dirPath += part + "/";
			}
		}
	}

		// Als je bijv. naar http://test.nl/dir gaat terwijl het eigenlijk http://test.nl/dir/index.html is
		File isDir = new File(url.getHost() + path);
		if ( isDir.isDirectory() )
		path += "/index.html";
	
		 //fstream = null; // Place to store the stream reference  
		try {
			FileOutputStream fstream = new FileOutputStream(url.getHost()+ path );
			fstream.write(content.getBytes());
			fstream.close();
	    } catch (FileNotFoundException e) {
	      e.printStackTrace(System.err);
	    }
	}	
	
	/**
	 * @param args
	 * @throws URISyntaxException 
	 */
	public static void main(String[] args) throws URISyntaxException {
		//WebsiteRipper ripper = 
			new WebsiteRipper();
	}
	
}
